package com.abu.edums.controller;


import com.abu.edums.client.VodClient;
import com.abu.edums.mapper.VideoMapper;
import com.abu.pojo.Course;
import com.abu.pojo.CourseDescription;
import com.abu.pojo.CourseQuery;
import com.abu.pojo.Video;
import com.abu.vo.CourseInfoVO;
import com.abu.vo.CoursePublishVo;
import com.abu.edums.service.CourseDescriptionService;
import com.abu.edums.service.CourseService;
import com.abu.utils.Resp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2020-06-18
 */
@RestController
@RequestMapping("/eduservice/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private VodClient vodClient;

    @PostMapping("addCourseInfo")
    public Resp addCourseInfo(@RequestBody CourseInfoVO courseInfoVO) {
        String courseId = this.courseService.saveCourseInfo(courseInfoVO);
        if (StringUtils.isNotBlank(courseId)) {
            return Resp.ok().data("courseId", courseId);
        }
        return Resp.error();
    }

    /**
     * 查询课程信息和描述信息
     *
     * @param cid
     * @return
     */
    @GetMapping("getCourseInfo/{cid}")
    public Resp getCourseInfoVO(@PathVariable("cid") String cid) {
        Course course = this.courseService.getById(cid);
        CourseDescription courseDescription = this.courseDescriptionService.getById(cid);
        CourseInfoVO courseInfoVO = new CourseInfoVO();
        if (course != null) {
            BeanUtils.copyProperties(course, courseInfoVO);
        }
        courseInfoVO.setDescription(courseDescription.getDescription());
        return Resp.ok().data("courseInfoVo", courseInfoVO);
    }

    @PostMapping("updateCourseInfo")
    public Resp updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO) {
        String courseId = this.courseService.updateCourseInfoVO(courseInfoVO);
        if (StringUtils.isNotBlank(courseId)) {
            return Resp.ok().data("courseId", courseId);
        }
        return Resp.error();
    }

    @GetMapping("getPublishCourseInfo/{courseId}")
    public Resp getCoursePublishInfo(@PathVariable("courseId") String courseId) {
        CoursePublishVo courseInfoVO = this.courseService.getCoursePublishInfo(courseId);
        if (courseInfoVO == null) {
            return Resp.error();
        }
        return Resp.ok().data("publishCourse", courseInfoVO);
    }

    @PostMapping("publishCourse/{courseId}")
    public Resp CoursePublishUltimate(@PathVariable("courseId") String courseId) {
        boolean flag = this.courseService.update(new UpdateWrapper<Course>().eq("id", courseId).set("status", "Normal"));
        if (flag) {
            return Resp.ok();
        }
        return Resp.error();
    }

    @PostMapping("pageCourseCondition/{page}/{size}")
    public Resp getAllCourse(
            @PathVariable("page") Long page,
            @PathVariable("size") Long size,
            @RequestBody(required = false) CourseQuery courseQuery
    ) {
        HashMap<String, Object> courseMap = this.courseService.queryPageCourse(page, size,courseQuery);
        if (CollectionUtils.isEmpty(courseMap)) {
            return Resp.error();
        }
        return Resp.ok().data(courseMap);
    }

    @Transactional()
    @DeleteMapping("delete/{courseId}")
    public Resp deletePublishCourseById(@PathVariable("courseId") String courseId) {
//        1.删除课程中所有小节的云端视频
        List<Video> videoList = this.videoMapper.selectList(new QueryWrapper<Video>().eq("course_id", courseId).select("video_source_id"));
        if (!CollectionUtils.isEmpty(videoList)) {
//            1.1获取所有的videoSourceId
            List<String> videoSourceIds = videoList.stream().map(Video::getVideoSourceId).collect(Collectors.toList());
//            1.2将videoSourceIds拼接成字符串
            String videoSourceIdsString = StringUtils.join(videoSourceIds, ",");
//            1.3执行删除操作
            this.vodClient.removeAlyVideo(videoSourceIdsString);
        }
//        2.删除数据库中的数据
        Boolean flag = this.courseService.deletePublishCourseById(courseId);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error();
    }

    @GetMapping("index/courses")
    public List<Course> queryIndexCourses() {
        List<Course> courseList = this.courseService.queryLimitById();
        if (CollectionUtils.isEmpty(courseList)) {
            return null;
        }
        return courseList;
    }

}

